﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.Database.Service;

namespace T.FAS.Runtime.Page.Dao
{
    public class PageUIControlLogic
    {
        readonly PageUIControlDao pageUIControlDao = new PageUIControlDao();
        readonly PageCSSDao pageCSSDao = new PageCSSDao();
        /// <summary>
        /// 新增
        /// </summary>
        public void AddPageUIControls(List<PageUIControl> pageUIControls)
        {
            pageUIControlDao.Add(pageUIControls);
            foreach (var pageUIControl in pageUIControls)
            {
                int order = 0;
                if (pageUIControl.Children != null)
                {
                    foreach (var item in pageUIControl.Children)
                    {
                        item.ID = Guid.NewGuid().ToString();
                        item.PageID = pageUIControl.PageID;
                        item.ControlOrder = order++;
                        item.ParentID = pageUIControl.ID;
                    }
                    AddPageUIControls(pageUIControl.Children);
                }
                if (pageUIControl.CSS != null)
                {
                    PageCSS pageCSS = pageUIControl.CSS;
                    if (pageCSS.ClassList != null && pageCSS.ClassList.Count > 0)
                    {
                        pageCSS.CustomClass = string.Join(",", pageCSS.ClassList);
                    }
                    pageCSS.PageID = pageUIControl.PageID;
                    pageCSS.ID = pageUIControl.ID;
                    pageCSSDao.Add(new List<object> { pageCSS });
                }
            }
        }
        /// <summary>
        /// 获取控件
        /// </summary>
        public List<PageUIControl> GetPageUIControls(string parentID, List<PageUIControl> pageUIControls, List<PageCSS> pageCSSs)
        {
            List<PageUIControl> controls = pageUIControls.Where(t => t.ParentID == parentID).OrderBy(t => t.ControlOrder).ToList();
            foreach (var item in controls)
            {
                item.Children = GetPageUIControls(item.ID, pageUIControls,  pageCSSs);
                item.CSS = pageCSSs.FirstOrDefault(t => t.ID == item.ID);
                if (item.CSS != null)
                {
                    if (!string.IsNullOrEmpty(item.CSS.CustomClass))
                    {
                        item.CSS.ClassList = item.CSS.CustomClass.Split(',').ToList();
                    }
                    else
                    {
                        item.CSS.ClassList = new List<string>();
                    }
                }
                else
                {
                    item.CSS = new PageCSS();
                }
            }
            return controls;
        }
        /// <summary>
        /// 获取控件
        /// </summary>
        public List<PageUIControl> GetRTPPageUIControls(string parentID, List<PageUIControl> pageUIControls, List<PageEvent> pageEvents, List<PageCSS> pageCSSs)
        {
            List<PageUIControl> controls = pageUIControls.Where(t => t.ParentID == parentID).OrderBy(t => t.ControlOrder).ToList();
            foreach (var item in controls)
            {
                item.Children = GetRTPPageUIControls(item.ID, pageUIControls, pageEvents, pageCSSs);
                item.Events = pageEvents.Where(t => t.InstanceCode == item.InstanceCode).OrderBy(t => t.EventOrder).ToList();
                item.CSS = pageCSSs.FirstOrDefault(t => t.ID == item.ID);
                if (item.CSS != null)
                {
                    if (!string.IsNullOrEmpty(item.CSS.CustomClass))
                    {
                        item.CSS.ClassList = item.CSS.CustomClass.Split(',').ToList();
                    }
                    else
                    {
                        item.CSS.ClassList = new List<string>();
                    }
                }
                else
                {
                    item.CSS = new PageCSS();
                }
            }
            return controls;
        }
    }
}
